package com.example.tibetanmedicinechest.controller;


import com.example.tibetanmedicinechest.mapper.FavoriteMapper;
import com.example.tibetanmedicinechest.model.Code;
import com.example.tibetanmedicinechest.model.Favorite;
import com.example.tibetanmedicinechest.model.Result;
import com.example.tibetanmedicinechest.service.IFavoriteService;
import com.example.tibetanmedicinechest.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 编程笨蛋
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/favorite")
public class FavoriteController {
    @Autowired
    IFavoriteService iFavoriteService;
    @PostMapping
    public Result addFavorite(@RequestBody Favorite favorite){
        boolean flag = iFavoriteService.addFavorite(favorite.getCase_id());
        if(flag){
            return ResultUtil.getSuccess(flag);
        } else{
            return ResultUtil.getError(Code.BUSINESS_ERR,"添加失败");
        }
    }
    @DeleteMapping
    public Result deleteFavorite(@RequestBody Favorite favorite){
        boolean flag = iFavoriteService.deleteFavorite(favorite.getCase_id());
        if(flag){
            return ResultUtil.getSuccess(flag);
        } else{
            return ResultUtil.getError(Code.BUSINESS_ERR,"删除失败");
        }
    }

    @GetMapping("/getFavoriteById")
    public Result getCaseById(Integer user_id){
        List<Map<String, Object>> maps = iFavoriteService.selectFavoriteById(user_id);
        if(maps.size()!=0){
            return ResultUtil.getSuccess(maps);
        } else {
            return ResultUtil.getError(Code.BUSINESS_ERR,"该用户没有收藏任何病例");
        }
    }
}

